window._MXYT = {

    initButton:function(uiNode){

        var btns = uiNode.getChildren();
        for(var i=0;i<btns.length;i++){
            btns[i].btnIndex = i;
            btns[i].on(cc.Node.EventType.TOUCH_START,function(){
               
                _MXYT.taskClick(this.btnIndex);

            },btns[i]);
        }

    },


    refreshUi:function(uiNode){
        var workList = window.gameData.workList;
        var btns = uiNode.getChildren();
        for(var i=0;i<btns.length;i++){
            btns[i].active = false;
        }

        for(var i=0;i<workList.length;i++){
            _MXYT.refreshTaskNode(workList[i],btns[i]);    
        }

    },

    refreshTaskNode:function(task,node){
        var signNodeYes = node.getChildByName("donhua")
                            .getChildByName("fuhao")
                            .getChildByName("huan");
        var signNodeNo =  node.getChildByName("donhua")
                        .getChildByName("fuhao")
                        .getChildByName("hong");
        var titleNode = node.getChildByName('title');
        var faceNode = node.getChildByName('face');
        var face = _MXYT.getFace(task);
        node.active = true;

        if(_MXYT.isTaskComplete(task)){

            signNodeYes.active = true;

            signNodeNo.active = false;

        }else{

            signNodeYes.active = false;

            signNodeNo.active = true;

        }

        titleNode.getComponent(cc.Label).string = task.taskname;

        if(task.main){
            titleNode.color = new cc.color("#FFDB00");
        }else{
            titleNode.color = new cc.color("#FFFFFF");
        }

        faceNode.getChildByName('img').getComponent(cc.Sprite).spriteFrame 
        = _GRM.getFaceFrame(face);
    },

    completeTask:function(){
        if(window.gameData.workList
            &&window.gameData.workList.length>0
        ){
            window.gameData.workList[0].complete=1;
        }
    },

    isTaskComplete:function(task){

       if(task.complete){
            return true;
        }else{
            return false;
        }

    },

    getFace:function(task){

        var detail = task.detail;
        var target = detail[0];
        return _NPC[target.target].face;

    },

    completeAllTask:function(){

        var workList = window.gameData.workList;
        for(var i=0;i<workList.length;i++){
            var task = workList[i];
            var detail = task.detail;
            for(var j=0;j<detail.length;j++){
                detail[j].current=detail[j].nmb;
            }
        }

        _MXYT.refreshUi(_MAP.taskListNode);
    },

    taskClick:function(taskIndex){

        console.log(taskIndex);
        var task = window.gameData.workList[taskIndex];
        console.log(task);
        //_TIPS.showMessage(_MXYT.getTaskDesc(task));
        //_MXYT.getReward(task);
        if(_MXYT.isTaskComplete(task)){
            _MTT.showUi(_MAP.node);
        }else{
            _TIPS.showMessage(_MXYT.getTaskDesc(task));
        }
    },

    getTaskDesc:function(task){
        return task.tips;
    },

    getReward:function(workInfo){
        var option = ['确定'];
        if(workInfo&&workInfo.reward){

            var message = "任务完成,获得:";

            for(var i=0;i<workInfo.reward.length;i++){

                var rewardObj = workInfo.reward[i];

                if(rewardObj.type=="gold"){
                    message+=" 钱*"+rewardObj.nmb;
                    _GAMEDATAHELPER.oprMoney(rewardObj.nmb);
                }else if(rewardObj.type=="exp"){
                    message+=" 经验*"+rewardObj.nmb;
                    for(var j=0;j<window.gameData.playerTeam.length;j++){
                        if(window.gameData.playerTeam[j].hp>0
                        &&!window.gameData.playerTeam[j].isCustomRole){
                            _MCRD.getRewardExp(window.gameData.playerTeam[j],null,rewardObj.nmb,null);
                        }
                    }
                }else if(
                        rewardObj.type=="getNpc"
                        &&rewardObj.value
                        &&_NPC[rewardObj.value]
                ){
                    message+=" "+rewardObj.value+"*1";
                    var tmpnpc = _CROF.getRandomMainRole(false,rewardObj.value,null);
                    tmpnpc.exp=400;
                    //点亮图鉴
                    //_GAMEDATAHELPER.signNpcTj(rewardObj.value);
                    //_GAMEDATAHELPER.signXwTjByRole(tmpnpc);
                    window.gameData.playerTeamBack.unshift(tmpnpc);
                }else{
                    for(var j=0;j<rewardObj.nmb;j++){
                        _GAMEDATAHELPER.pushItem(_IOF.createObjByName(rewardObj.type,null));
                    }
                    message+=" "+rewardObj.type+"*"+rewardObj.nmb;
                }

            }

            if(workInfo.nexttask&&workInfo.nexttask.length>0){
                if(workInfo.nexttask.length==1){
                    var taskname = workInfo.nexttask[0];
                    message+=" 解锁任务:"+taskname;
                    _MXYT.pushTask(taskname);
                    var nexttask = _MAINTASK[taskname];
                    console.log(nexttask,nexttask.map,_MAPCONFIG[nexttask.map]);
                    if(nexttask){
                        if(
                            nexttask.map
                            &&_MAPCONFIG[nexttask.map]
                            &&_MAPCONFIG[nexttask.map].isRoot
                        ){
                            message+=" 解锁地图:"+nexttask.map;
                        }
                    }
                }else{
                    message+=" 请选择以下任务中的一个继续游戏:";
                    option=workInfo.nexttask;
                }
            }else{
                message+=" 所有任务都已完成,可以在开始游戏时选择通关存档开启二周目！";
                window.gameData.workList=[]
            }

            //_TIPS.showMessage(message,0);

            //_MXYT.refreshUi(_MAP.taskListNode);

            return {msg:message,option:option};
        }

        return {msg:"未知任务",option:option};

    },

    getTaskLv:function(taskName){
        var lv = 5;
        var taskConfig = _MAINTASK[taskName];
        if(taskConfig&&_NPCMAP[taskConfig.map]){
            var i = _NPCMAP[taskConfig.map].lv;
            if(i<10){
                lv=5;
            }else if(i<20){
                lv=50;
            }else if(i<30){
                lv=100;
            }else if(i<40){
                lv=200;
            }else if(i<50){
                lv=300;
            }else if(i<60){
                lv=400;
            }else if(i<70){
                lv=500;
            }else if(i<80){
                lv=600;
            }else if(i<90){
                lv=700;
            }else if(i<100){
                lv=800;
            }else if(i<110){
                lv=800;
            }else if(i<120){
                lv=900;
            }else if(i<130){
                lv=900;
            }else{
                lv=1000;
            }
        }

        return lv;

    },

    getTaskNpc(taskName){
        var taskConfig = _MAINTASK[taskName];
        if(taskConfig&&taskConfig.detail&&taskConfig.detail.length>0){
            return taskConfig.detail[0].target;
        }
        return "???";
    },

    pushTask:function(name){
        var taskObj = _MXYT.getMainTaskByName(name);
        window.gameData.workList=[];
        window.gameData.workList.push(taskObj);
        if(
            taskObj.map
            &&_MAPCONFIG[taskObj.map]
            &&_MAPCONFIG[taskObj.map].isRoot
        ){
            _GAMEDATAHELPER.openChapter(taskObj.map);
        }
    },

    getNpcRewardOfTask:function(taskConf){

        var detail = taskConf.detail;
        var bossarr = [];
        var npcarr = [];
        var rewardnpc = null;
        for(var i=0;i<detail.length;i++){
            if(detail[i].nmb){
                npcarr.push(detail[i].target);
            }else{
                bossarr.push(detail[i].target);
            }
        }
        if(bossarr.length>0&&npcarr.length>0){
            if(Math.random()<0.1){
                rewardnpc=_CU.randomObj(bossarr);
            }else{
                rewardnpc = _CU.randomObj(npcarr);
            }
        }else if(npcarr.length>0){
            rewardnpc = _CU.randomObj(npcarr);
        }
        if(rewardnpc==null){
            return null;
        }else{
            return {type:"getNpc",value:rewardnpc};
        }
    },

    getMainTaskByName:function(name){

        if(_MAINTASK[name]){

            var tasklv = _MXYT.getTaskLv(name);
            var cloneTask = _CU.cloneObj(_MAINTASK[name]);
            cloneTask.isMain = 1;
            if(_TASKREWARD[tasklv]){
                cloneTask.reward =_CU.cloneObj(_TASKREWARD[tasklv]);
                var rewardnpc = _MXYT.getNpcRewardOfTask(cloneTask);
                if(rewardnpc){
                    cloneTask.reward.push(rewardnpc);
                }
            }

            cloneTask.taskname = name;
            cloneTask.npc = _MXYT.getTaskNpc(name);
            cloneTask.complete=0;
            return cloneTask;
        }

    },

    doTask:function(name,nmb){

        var workList = window.gameData.workList;
        for(var i=0;i<workList.length;i++){
            var task = workList[i];
            var detail = task.detail;
            for(var j=0;j<detail.length;j++){
                if(detail[j].target==name){
                    if(!detail[j].current){
                        detail[j].current = 0;
                    }
                    detail[j].current+=nmb;
                }
            }
        }

    }
}